package com.lingCheng.avg.service.impl;


import com.lingCheng.avg.common.StorageData;
import com.lingCheng.avg.common.TaskData;
import com.lingCheng.avg.entity.Locate;
import com.lingCheng.avg.mapper.LocateMapper;
import com.lingCheng.avg.service.LocateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class LocateServiceImpl implements LocateService {
    private static int startIndex = 1;
    private static final int BATCH_SIZE = 66;
    private static boolean isLastPage = false;
    @Autowired
    private LocateMapper locateMapper;

    @Override
    public TaskData findStorageStatic() {
        Locate storage = locateMapper.findStorage();
        log.info("storage:{}",storage);
        TaskData taskData = new TaskData();
        taskData.setMaterial(storage.getMaterial());//原料
        taskData.setProduct(storage.getProduct());  //成品
        taskData.setUsed(storage.getUsed());        //已使用
        taskData.setEmpty(storage.getEmpty());      //未使用
        return taskData;
    }
    @Override
    public List<StorageData> findStorageStatus() {
        if (isLastPage) {
            startIndex = 1; // 如果是最后一页，则重置为第一页
            isLastPage = false;
        }
        List<Locate> list = locateMapper.findStorageStatus(startIndex);
        List<StorageData> storageDataList = new ArrayList<>();
        for (Locate locate : list) {
            StorageData storageData=new StorageData();
            storageData.setStorageId(locate.getLocateNo());
            if(!locate.getIsLoad().equals("0")){
                storageData.setIsLoad("1");
                storageData.setMaterialCode(locate.getMaterialGrade());
            } else {
                storageData.setIsLoad("0");
            }
            storageDataList.add(storageData);
        }

        // 更新起始位置和判断是否为最后一页
//        startIndex += BATCH_SIZE;
//        int totalCount = locateMapper.getTotalCount();
//        if (startIndex > totalCount || list.size() < BATCH_SIZE) {
//            startIndex = 1; // 重置为1，准备下一轮查询
//            isLastPage = true; // 标记为最后一页
//        }
        startIndex++;
        if(startIndex>=22){
            startIndex = 1;
            isLastPage = true;
        }

        return storageDataList;
    }


    @Override
    public List<StorageData> findStorageStatus2(int currentPage, int pageSize) {
        int startIndex = (currentPage - 1) * pageSize;
        List<Locate> list = locateMapper.findStorageStatus2(startIndex, pageSize);
        List<StorageData> storageDataList = new ArrayList<>();
        for (Locate locate : list) {
            StorageData storageData=new StorageData();
            storageData.setStorageId(locate.getX()+","+locate.getY()+","+locate.getZ());
            if(!locate.getIsLoad().equals("0")){
                storageData.setIsLoad("1");
            } else {
                storageData.setIsLoad("0");
            }
            storageDataList.add(storageData);
        }

        return storageDataList;
    }
}
